from typing import AsyncGenerator

from sqlmodel import SQLModel
from sqlalchemy.ext.asyncio import async_session
from sqlmodel.ext.asyncio.session import AsyncSession

from config.database import async_engine, AsyncSessionLocal

async def get_db():
    """
    每一个请求处理完毕后会关闭当前连接，不同的请求使用不同的连接

    :return:
    """
    async with AsyncSession(async_engine) as current_db:
        yield current_db

async def init_create_table():
    """
    应用启动时初始化数据库连接

    :return:
    """
    # logger.info('初始化数据库连接...')
    async with async_engine.begin() as conn:
        await conn.run_sync(SQLModel.metadata.create_all)
    # logger.info('数据库连接成功')
